import Flutter
import MJExtension

class AppPlugin {
    static func register(registry: FlutterPluginRegistry) {
        FlutterMethodChannel(name: "AppPlugin", binaryMessenger: registry.registrar(forPlugin: "AppPlugin")!.messenger()).setMethodCallHandler { methodCall, result in
            let method = methodCall.method
            let arguments = (methodCall.arguments as? NSDictionary) ?? NSDictionary()
            if self.doApp(method, arguments, result) { return }
            result(FlutterMethodNotImplemented)
        }
    }
    
    func toJson(_ obj: Any) -> String {
        if obj is [NSObject] {
            return String(data: try! JSONSerialization.data(withJSONObject: (obj as! [NSObject]).map { $0.mj_keyValues() }, options: []), encoding: .utf8)!
        } else {
            return String(data: try! JSONSerialization.data(withJSONObject: (obj as! NSObject).mj_keyValues()!, options: []), encoding: .utf8)!
        }
    }
}
